<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>8. Schleifen &mdash; PowerShell fuer Systemintegratoren</title>
    
    <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/translations.js"></script>
    <link rel="top" title="PowerShell fuer Systemintegratoren" href="index.html" />
    <link rel="next" title="9. Diagramme der strukturierten Programmierung" href="powershell_struktogramm.html" />
    <link rel="prev" title="7. Bedingungen" href="powershell_bedingung.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="Allgemeiner Index"
             accesskey="I">Index</a></li>
        <li class="right" >
          <a href="powershell_struktogramm.html" title="9. Diagramme der strukturierten Programmierung"
             accesskey="N">weiter</a> |</li>
        <li class="right" >
          <a href="powershell_bedingung.html" title="7. Bedingungen"
             accesskey="P">zurück</a> |</li>
        <li><a href="index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/PowerShell_icon_small.png" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Inhalt</a></h3>
  <ul>
<li><a class="reference internal" href="#">8. Schleifen</a><ul>
<li><a class="reference internal" href="#for-schleife">8.1. For-Schleife</a></li>
<li><a class="reference internal" href="#while-do-while">8.2. while, do - while</a></li>
<li><a class="reference internal" href="#foreach">8.3. foreach</a></li>
<li><a class="reference internal" href="#schleifen-verlassen">8.4. Schleifen verlassen</a></li>
<li><a class="reference internal" href="#schleifen-fortfuhren">8.5. Schleifen fortführen</a></li>
<li><a class="reference internal" href="#zusammenfassung">8.6. Zusammenfassung</a></li>
</ul>
</li>
</ul>

  <h4>Vorheriges Thema</h4>
  <p class="topless"><a href="powershell_bedingung.html"
                        title="vorheriges Kapitel">7. Bedingungen</a></p>
  <h4>Nächstes Thema</h4>
  <p class="topless"><a href="powershell_struktogramm.html"
                        title="nächstes Kapitel">9. Diagramme der strukturierten Programmierung</a></p>
  <h3>Diese Seite</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/powershell_schleifen.txt"
           rel="nofollow">Quelltext anzeigen</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Schnellsuche</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Los" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Geben Sie Suchbegriffe oder einen Modul-, Klassen- oder Funktionsnamen ein.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="schleifen">
<h1>8. Schleifen<a class="headerlink" href="#schleifen" title="Permalink zu dieser Überschrift">¶</a></h1>
<div class="topic">
<p class="topic-title first">Überblick</p>
<ul class="simple">
<li>Do und While</li>
<li>For</li>
<li>Foreach</li>
<li>Break/Continue</li>
<li>Nested Loops und Labels</li>
</ul>
</div>
<p>Neben der Abfrage von Bedingungen muss der Programmierer häufig durch Listen iterieren, so muss er z.B. häufig die Inhalte von Arrays ausgeben.</p>
<div class="section" id="for-schleife">
<span id="index-1"></span><h2>8.1. For-Schleife<a class="headerlink" href="#for-schleife" title="Permalink zu dieser Überschrift">¶</a></h2>
<a class="reference internal image-reference" href="_images/ps_for_1.png"><img alt="_images/ps_for_1.png" src="_images/ps_for_1.png" style="width: 500px;" /></a>
<p id="index-2">Die einfachste Art des Schleifendurchlaufs ist der mittels des <strong>for</strong>-Statements, gerne auch Zählschleife genannt. Es hat 3 grundlegende Elemente:</p>
<dl class="docutils">
<dt>Initialisierung:</dt>
<dd>Der erste Ausdruck wird beim Beginn der Schleife ausgewertet.</dd>
<dt>Wiederholungsbedingung:</dt>
<dd>Der zweite Ausdruck wird bei jedem neuen Schleifendurchlauf ausgewertet. Wenn der Ausdruck <strong>true</strong> ergibt, wird in die Schleife gesprungen.</dd>
<dt>Increment:</dt>
<dd>Der 3. Ausdruck wird bei Vorhandensein nach jedem Schleifendurchlauf ausgeführt. Häufig wird hier ein Zähler inkrementiert. Er ist aber nicht grundsätzlich notwendig.</dd>
</dl>
<a class="reference internal image-reference" href="_images/ps_loop.png"><img alt="_images/ps_loop.png" src="_images/ps_loop.png" style="width: 500px;" /></a>
<a class="reference internal image-reference" href="_images/ps_for.png"><img alt="_images/ps_for.png" src="_images/ps_for.png" style="width: 500px;" /></a>
</div>
<div class="section" id="while-do-while">
<span id="index-3"></span><h2>8.2. while, do - while<a class="headerlink" href="#while-do-while" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Do and While sind prinzipiell Endlosschleifen. Solange die Abbruchbedingung nicht true ergibt, werden beide Schleifen endlos durchlaufen.</p>
<div class="highlight-sh"><div class="highlight"><pre>Do <span class="o">{</span>
        <span class="nv">$input</span> <span class="o">=</span> Read-Host <span class="s2">&quot;Your homepage&quot;</span>
<span class="o">}</span> While <span class="o">(</span>!<span class="o">(</span><span class="nv">$input</span> -like <span class="s2">&quot;www.*.*&quot;</span><span class="o">))</span>



<span class="nv">$input</span> <span class="o">=</span> Read-Host <span class="s2">&quot;Your homepage&quot;</span>
<span class="k">while</span><span class="o">(</span>!<span class="o">(</span><span class="nv">$input</span> -like <span class="s2">&quot;www.*.*&quot;</span><span class="o">))</span>
<span class="o">{</span>
        <span class="nv">$input</span> <span class="o">=</span> Read-Host <span class="s2">&quot;Your homepage&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="foreach">
<span id="index-4"></span><h2>8.3. foreach<a class="headerlink" href="#foreach" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Die foreach-Anweisung iteriert über eine Liste von Objekten. Sie sollte nicht innerhalb einer Pipeline angwendet werden, weil sie dort auf das Ergebnis der Pipeline warten muss; innerhalb von Pipelines ist das foreach-Objekt die geeignete Wahl.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="c"># Create your own array:</span>
<span class="nv">$array</span> <span class="o">=</span> 3,6,<span class="s2">&quot;Hello&quot;</span>,12
<span class="c"># Read out this array element by element:</span>
Foreach <span class="o">(</span><span class="nv">$element</span> in <span class="nv">$array</span><span class="o">)</span>
<span class="o">{</span>
        <span class="s2">&quot;Current element: $element&quot;</span>
<span class="o">}</span>
Current element: 3
Current element: 6
Current element: Hello
Current element: 12
</pre></div>
</div>
</div>
<div class="section" id="schleifen-verlassen">
<span id="index-5"></span><h2>8.4. Schleifen verlassen<a class="headerlink" href="#schleifen-verlassen" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Mit Hilfe des <strong>break</strong>-Statements können Schleifen vorzeitig verlassen werden. Eine eventuell noch zu durchlaufende Abbruchbedingung wird nicht mehr ausgewertet.</p>
<div class="highlight-sh"><div class="highlight"><pre>While <span class="o">(</span><span class="nv">$true</span><span class="o">)</span>
<span class="o">{</span>
  <span class="nv">$password</span> <span class="o">=</span> Read-Host <span class="s2">&quot;Enter password&quot;</span>
  If <span class="o">(</span><span class="nv">$password</span> -eq <span class="s2">&quot;secret&quot;</span><span class="o">)</span> <span class="o">{</span><span class="nb">break</span><span class="o">}</span>
<span class="o">}</span>

For <span class="o">(</span><span class="nv">$i</span><span class="o">=</span>1; <span class="nv">$i</span> -lt 4; <span class="nv">$i</span>++<span class="o">)</span>
<span class="o">{</span>
  <span class="nv">$password</span> <span class="o">=</span> Read-Host <span class="s2">&quot;Enter password ($i. try)&quot;</span>
  If <span class="o">(</span><span class="nv">$password</span> -eq <span class="s2">&quot;secret&quot;</span><span class="o">)</span> <span class="o">{</span><span class="nb">break</span><span class="o">}</span>
  If <span class="o">(</span><span class="nv">$i</span> -ge 3<span class="o">)</span> <span class="o">{</span> Throw <span class="s2">&quot;The entered password was incorrect.&quot;</span> <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="schleifen-fortfuhren">
<span id="index-6"></span><h2>8.5. Schleifen fortführen<a class="headerlink" href="#schleifen-fortfuhren" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Mit Hilfe von <strong>continue</strong> können Sie den jeweils aktuellen Schleifendurchlauf beenden, ohne die Schleife selbst zu beenden.</p>
<p>Das folgende Beispiel gibt nur die Länge von Dateien aus; sollte es auf Ordner stoßen, werden diese sofort übersprungen.</p>
<div class="highlight-sh"><div class="highlight"><pre>Foreach <span class="o">(</span><span class="nv">$entry</span> in Dir <span class="nv">$env</span>:windir<span class="o">)</span>
<span class="o">{</span>
  <span class="c"># If the current element matches the desired type,</span>
  <span class="c"># continue immediately with the next element:</span>
  If <span class="o">(</span>!<span class="o">(</span><span class="nv">$entry</span> -is <span class="o">[</span>System.IO.FileInfo<span class="o">]))</span> <span class="o">{</span> Continue <span class="o">}</span>
  <span class="s2">&quot;File {0} is {1} bytes large.&quot;</span> -f <span class="nv">$entry</span>.name, <span class="nv">$entry</span>.length
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="zusammenfassung">
<h2>8.6. Zusammenfassung<a class="headerlink" href="#zusammenfassung" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>The cmdlet ForEach-Object gives you the option of processing single objects of the PowerShell pipeline, such as to output the data contained in object properties as text or to invoke methods of the object.</p>
<p>Foreach is a similar type of loop whose contents do not come from the pipeline, but from an array or a collection.
In addition, there are endless loops that iterate a code block until a particular condition is met.</p>
<p>The simplest type of such loops is While, in which continuation criteria are checked at the beginning of the loop. If you want to do the checking at the end of the loop, choose Do...While. The For loop is an extended While loop, because it can count loop cycles and automatically terminate the loop after a designated number of iterations. This means that For is suited mainly for loops in which counts are to be made or which must complete a set number of iterations.</p>
<p>Do...While and While, on the other hand, are suited for loops
that have to be iterated as long as the respective situation and running time conditions require it.</p>
<p>Finally, Switch is a combined Foreach loop with integrated conditions so that you can immediately implement different actions independently of the read element. Moreover, Switch can step through the contents of text files line by line and evaluate even log files of substantial size.</p>
<p>All loops can exit ahead of schedule with the help of Break and skip the current loop cycle with the help of Continue. In the case of nested loops, you can assign an unambiguous name to the loops and then use this name to apply Break or Continue to nested loops.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="Allgemeiner Index"
             >Index</a></li>
        <li class="right" >
          <a href="powershell_struktogramm.html" title="9. Diagramme der strukturierten Programmierung"
             >weiter</a> |</li>
        <li class="right" >
          <a href="powershell_bedingung.html" title="7. Bedingungen"
             >zurück</a> |</li>
        <li><a href="index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, STE.
      Mit <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1 erstellt.
    </div>
  </body>
</html>